package com.magic.gre.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.magic.gre.R;
import com.magic.gre.adapter.ActivityAdapter;
import com.magic.gre.base.activity.BaseMVPActivity;
import com.magic.gre.entity.ActivityBean;
import com.magic.gre.helper.Contract;
import com.magic.gre.mvp.contract.MyActivityContract;
import com.magic.gre.mvp.presenter.MyActivityPresenterImpl;
import com.magic.gre.ui.dialog.AvtivityDialog;
import com.scwang.smartrefresh.layout.api.RefreshLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Author: Lmoumou
 * Date: 2019-01-10 20:16
 * 文件名: MyActivityActivity
 * 描述:
 */
public class MyActivityActivity extends BaseMVPActivity<MyActivityPresenterImpl> implements MyActivityContract.View,
        ActivityAdapter.OnActivityitemListener {

    @BindView(R.id.mRecyclerView)
    RecyclerView mRecyclerView;

    public static void startThis(Context context) {
        Intent intent = new Intent(context.getApplicationContext(), MyActivityActivity.class);
        context.startActivity(intent);
    }

    private int pageNo;
    private ActivityAdapter adapter;
    private List<ActivityBean> dataList = new ArrayList<>();

    @Override
    protected int getLayoutId() {
        return R.layout.activity_my_activity;
    }

    @Override
    protected MyActivityPresenterImpl creatPresenter() {
        return new MyActivityPresenterImpl(this);
    }

    @Override
    protected void netOverride() {

    }

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new ActivityAdapter(this, dataList,true);
        adapter.setOnActivityitemListener(this);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.setAdapter(adapter);

        pageNo = 1;
        mPresenter.pActivityList(pageNo);
    }

    @Override
    public void onItemClick(String url, String activityId) {
        mPresenter.pActDetails(activityId, "1");
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        super.onRefresh(refreshLayout);
        pageNo = 1;
        mPresenter.pActivityList(pageNo);
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        super.onLoadMore(refreshLayout);
        mPresenter.pActivityList(pageNo);
    }

    @Override
    public void onSignUp(ActivityBean bean) {
        AvtivityDialog dialog = new AvtivityDialog();
        dialog.showThis(getSupportFragmentManager(), AvtivityDialog.class.getSimpleName(), bean.getId(), bean.getApplyImage());
    }

    @Override
    public void vActivitylist(List<ActivityBean> beanList) {
        if (pageNo == 1) {
            dataList.clear();
            if (beanList == null || beanList.isEmpty()) {
                adapter.notifyDataSetChanged();
                stopRefresh(true, false);
                showEmpty();
                return;
            }
        }
        pageNo++;
        dataList.addAll(beanList);
        adapter.notifyDataSetChanged();
        stopRefresh(true, beanList.size() == Contract.PAGER_SIZE);
        showContent();
    }

    @Override
    public void vActDetails(ActivityBean bean, String activityId) {
        ActivityDetailsActivity.startThis(this, bean, activityId,true);
    }
}
